package com.research.retry.service;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalTime;
//https://www.jianshu.com/p/314059943f1c
@Slf4j
@Service
public class PayService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    private final int totalNum = 100000;


    @Async
    @Retryable(value = Exception.class,maxAttempts = 4,backoff = @Backoff(delay = 2000,multiplier = 1.5))
    public int minGoodsnum(int num){
        log.info("减库存开始=>" + LocalTime.now());
        log.info("==============注意开始重试了==============");

        log.info("库存=>" + totalNum);

        if(num <= 0){
            throw new IllegalArgumentException("数量不对");
        }
        log.info("减库存执行结束=>" + LocalTime.now());
        return totalNum - num;
    }

    /**
     * 使用@Recover注解，当重试次数达到设置的次数的时候，还是失败抛出异常，执行的回调函数。
     * @param e
     * @return
     */
    @Recover
    public int recover(Exception e){
        log.warn("减库存失败！！！" + LocalTime.now());
        //记日志到数据库
        return totalNum;
    }

    public void method(int a){
        System.out.println("这是一个测试");
    }
}
